import logging
import os.path


# import colorlog


class MotorLogger:
    def __init__(self):
        system_separator = os.sep
        current_dir = os.path.dirname(os.path.abspath(__file__))
        self.logger = logging.getLogger('MotorLogger')
        self.logger.setLevel(logging.DEBUG)

        # self.log_color_config = {
        #     'DEBUG': 'cyan',
        #     'INFO': 'green',
        #     'WARNING': 'yellow',
        #     'ERROR': 'red',
        #     'CRITICAL': 'red',
        # }
        # create a handler for write
        file_handler = logging.FileHandler(current_dir + system_separator + 'motor_logger.log')
        # file_handler.setLevel(logging.DEBUG)
        # create a handler for control
        console_handler = logging.StreamHandler()
        # console_handler.setLevel(logging.INFO)

        # set format
        formatter = logging.Formatter(
            '[%(asctime)s -%(name)s - line number: %(lineno)s] -[thread name: %(threadName)s] -%(levelname)s -%(message)s')
        # formatter = colorlog.ColoredFormatter('%(log_color)s%(asctime)s - %(name)s - line number: %(lineno)s -thread name: %(threadName)s- - %(levelname)s - %(message)s',log_colors=self.log_color_config)

        file_handler.setFormatter(formatter)
        console_handler.setFormatter(formatter)

        self.logger.addHandler(file_handler)
        self.logger.addHandler(console_handler)

    def info_log(self, msg):
        # 绿色
        # self.logger.info("\033[0;32m" + msg + "\033[0m")
        self.logger.info(msg)

    def debug_log(self, msg):
        # 灰色
        # self.logger.debug("\033[0;37m" + msg + "\033[0m")
        self.logger.debug(msg)

    def error_log(self, msg):
        # 红色
        # self.logger.error("\033[0;31m" + msg + "\033[0m")
        self.logger.error(msg)

    def critical_log(self, msg):
        # 红色
        # self.logger.critical("\033[0;31m" + msg + "\033[0m")
        self.logger.critical(msg)

    def warning_log(self, msg):
        # 黄色
        # self.logger.warning("\033[0;33m" + msg + "\033[0m")
        self.logger.warning(msg)


if __name__ == '__main__':
    motor_logger = MotorLogger()
    motor_logger.debug_log("Motor logger debug log")
    motor_logger.info_log(f"Motor logger info log")
    motor_logger.error_log(f"Motor logger info log")
    motor_logger.info_log(f"twice Motor logger info log")
    pass
